﻿using System;
using System.Collections.Generic;
using System.Data;
using System.Data.Entity;
using System.Linq;
using System.Web;
using System.Web.Mvc;
using Comukrte;

namespace Comukrte.Controllers
{ 
    public class PersonalController : Controller
    {
        private comukrteEntities db = new comukrteEntities();
        //
        // GET: /CursosAsistidos/
        [Authorize(Roles = "Administrador,Recursos Humanos")]
        public ViewResult CursosAsistidos(int id)
        {
            var vCursosAsistidos = db.tblPersonal.Where(m => m.PersonaId == id);            
            return View(vCursosAsistidos);
        }
        //
        // GET: /Personal/
        [Authorize(Roles = "Administrador,Recursos Humanos")]
        public ViewResult Index()
        {
            var tblpersonal = db.tblPersonal.Include(t => t.tblAreas).Include(t => t.tblPuestos);
            return View(tblpersonal.ToList());
        }

        //
        // GET: /Personal/Details/5
        [Authorize(Roles = "Administrador,Recursos Humanos")]
        public ViewResult Details(int id)
        {
            tblPersonal tblpersonal = db.tblPersonal.Find(id);
            return View(tblpersonal);
        }

        //
        // GET: /Personal/Create
        [Authorize(Roles = "Administrador,Recursos Humanos")]
        public ActionResult Create()
        {
            ViewBag.AreaId = new SelectList(db.tblAreas, "AreaId", "Nombre");
            ViewBag.PuestoId = new SelectList(db.tblPuestos, "PuestoId", "Nombre");
            return View();
        } 

        //
        // POST: /Personal/Create
        [Authorize(Roles = "Administrador,Recursos Humanos")]
        [HttpPost]
        public ActionResult Create(tblPersonal tblpersonal)
        {
            if (ModelState.IsValid)
            {
                db.tblPersonal.Add(tblpersonal);
                db.SaveChanges();
                return RedirectToAction("Index");  
            }

            ViewBag.AreaId = new SelectList(db.tblAreas, "AreaId", "Nombre", tblpersonal.AreaId);
            ViewBag.PuestoId = new SelectList(db.tblPuestos, "PuestoId", "Nombre", tblpersonal.PuestoId);
            return View(tblpersonal);
        }
        
        //
        // GET: /Personal/Edit/5
 [Authorize(Roles = "Administrador,Recursos Humanos")]
        public ActionResult Edit(int id)
        {
            tblPersonal tblpersonal = db.tblPersonal.Find(id);
            ViewBag.AreaId = new SelectList(db.tblAreas, "AreaId", "Nombre", tblpersonal.AreaId);
            ViewBag.PuestoId = new SelectList(db.tblPuestos, "PuestoId", "Nombre", tblpersonal.PuestoId);
            return View(tblpersonal);
        }

        //
        // POST: /Personal/Edit/5
        [Authorize(Roles = "Administrador,Recursos Humanos")]
        [HttpPost]
        public ActionResult Edit(tblPersonal tblpersonal)
        {
            if (ModelState.IsValid)
            {
                db.Entry(tblpersonal).State = EntityState.Modified;
                db.SaveChanges();
                return RedirectToAction("Index");
            }
            ViewBag.AreaId = new SelectList(db.tblAreas, "AreaId", "Nombre", tblpersonal.AreaId);
            ViewBag.PuestoId = new SelectList(db.tblPuestos, "PuestoId", "Nombre", tblpersonal.PuestoId);
            return View(tblpersonal);
        }

        //
        // GET: /Personal/Delete/5
 [Authorize(Roles = "Administrador,Recursos Humanos")]
        public ActionResult Delete(int id)
        {
            tblPersonal tblpersonal = db.tblPersonal.Find(id);
            return View(tblpersonal);
        }

        //
        // POST: /Personal/Delete/5
        [Authorize(Roles = "Administrador,Recursos Humanos")]
        [HttpPost, ActionName("Delete")]
        public ActionResult DeleteConfirmed(int id)
        {            
            tblPersonal tblpersonal = db.tblPersonal.Find(id);
            db.tblPersonal.Remove(tblpersonal);
            db.SaveChanges();
            return RedirectToAction("Index");
        }

        protected override void Dispose(bool disposing)
        {
            db.Dispose();
            base.Dispose(disposing);
        }
    }
}